function focusInput(inputId) {
    document.getElementById(inputId).focus();
}

function addStudent() {
    var form = document.getElementById('studentForm');
    var name = document.getElementById('name').value;
    var age = document.getElementById('age').value;
    var gender = document.querySelector('input[name="gender"]:checked').value;
    var sid = document.getElementById('sid').value;
    var classSelected = document.getElementById('class').value;

    // 校验输入内容
    if (name === "" || age === "" || sid === "") {
        alert("学生信息校验失败，请检查输入内容！");
        return;
    }

    // 创建新的表格行并添加拖放支持
    var table = document.getElementById('studentTable').getElementsByTagName('tbody')[0];
    var newRow = table.insertRow();
    newRow.draggable = true;
    newRow.addEventListener('dragstart', function(event) {
        event.dataTransfer.setData("text/plain", event.target.id);
    });
    newRow.addEventListener('dragover', function(event) {
        event.preventDefault();
    });
    newRow.addEventListener('drop', function(event) {
        event.preventDefault();
        var data = event.dataTransfer.getData("text/plain");
        var sourceRow = document.getElementById(data);
        var targetRow = event.target.parentElement;
        swapRows(sourceRow, targetRow);
    });

    // 在新行中插入单元格
    var cell1 = newRow.insertCell(0);
    var cell2 = newRow.insertCell(1);
    var cell3 = newRow.insertCell(2);
    var cell4 = newRow.insertCell(3);
    var cell5 = newRow.insertCell(4);
    cell1.innerHTML = name;
    cell2.innerHTML = age;
    cell3.innerHTML = gender;
    cell4.innerHTML = sid;
    cell5.innerHTML = classSelected;

    // 清空表单内容
    document.getElementById('name').value = "";
    document.getElementById('age').value = "";
    document.getElementById('male').checked = false;
    document.getElementById('female').checked = false;
    document.getElementById('sid').value = "";
    document.getElementById('class').selectedIndex = 0;
}

function swapRows(sourceRow, targetRow) {
    var sourceCells = Array.from(sourceRow.cells);
    var targetCells = Array.from(targetRow.cells);

    sourceCells.forEach((cell, index) => {
        var temp = cell.innerHTML;
        cell.innerHTML = targetCells[index].innerHTML;
        targetCells[index].innerHTML = temp;
    });
}


function deleteStudent() {
    var table = document.getElementById('studentTable');
    var rowCount = table.rows.length;
    for (var i = rowCount - 1; i > 0; i--) {
        table.deleteRow(i);
    }
}

function showStatistics() {
    var table = document.getElementById("studentTable");
    var totalStudents = table.rows.length - 1; // 减去表头
    var maleCount = 0;
    var femaleCount = 0;
    for (var i = 1; i < table.rows.length; i++) {
        var gender = table.rows[i].cells[2].innerHTML;
        if (gender === "男") {
            maleCount++;
        } else {
            femaleCount++;
        }
    }
    var malePercentage = (maleCount / totalStudents) * 100;
    var femalePercentage = (femaleCount / totalStudents) * 100;
    alert("学生总数: " + totalStudents + "\n男生比例: " + malePercentage.toFixed(2) + "%\n女生比例: " + femalePercentage.toFixed(2) + "%");
}

function allowDrop(event) {
    event.preventDefault();
}

function drop(event) {
    event.preventDefault();
    var data = event.dataTransfer.getData("text");
    var sourceRow = document.getElementById(data);
    var targetRow = event.target.closest("tr");


    if (sourceRow && targetRow && sourceRow !== targetRow) {
        var parent = targetRow.parentNode;
        var next = targetRow.nextSibling === sourceRow ? targetRow : targetRow.nextSibling;
        parent.insertBefore(sourceRow, next);
    }
}





